import axios from "axios";
import { toast } from "svelte-sonner";
import * as jsonpatch from "fast-json-patch";
import type ArrowUp_0_1 from "@lucide/svelte/icons/arrow-up-0-1";

export const debugBy =
  (src: string) =>
  (...data: any[]) =>
    console.debug(src, data);
export const toastErr = (err: any) => toast.error(err.message);
export type User = { id: number; username: string; avatar: string };
export type Page = { cur: number; num: number };
export type LisRes = { data: any[]; total: number };
export const axiosIns = axios.create({
  baseURL: `/cool_course/api`,
  timeout: 5000,
  auth: {
    username: import.meta.env.VITE_USER_ROLE,
    password: "",
  },
});
export const space_patch_key = (space_id: number) =>
  `space:state:patch:${space_id}`;
export const space_perms_key = (space_id: number) => `space:perms:${space_id}`;
export const space_pings_key = (space_id: number) => `space:pings:${space_id}`;
export const topic_pings = "space:pings";
export const topic_perms = "space:perms";
export const topic_patch = "space:patch";
export type Space = {
  id: number;
  owner_id: number;
  owner_name: string;
  name: string;
  desp?: string;
  booking: boolean;
  booking_status?: boolean;
  users: SpaceUser[];
  state: SpaceState;
};
export const patchState = () => {
  let space: any = {};
  let observer = jsonpatch.observe<Object>(space);
  space.a = 1;
  let p1 = jsonpatch.generate(observer);
  console.debug(p1);
  space.b = 2;
  let p2 = jsonpatch.generate(observer);
  console.debug(p2);
  jsonpatch.unobserve(space, observer);
};
export type SpaceUser = {
  id: number;
  username: string;
  avator: string;
  perm: number;
  online: boolean;
};
export type SpaceState = {
  boards: Board[];
  active_id?: string;
};
export type Board = {
  id: string;
  label: string;
  rect: Rect;
  panels: Panel[];
};
export const newBoard = () => ({
  id: "",
  label: "",
  rect: newRect(),
  panels: [],
});
export type Panel = {
  id: string;
  kind: string;
  label: string;
  rect: Rect;
  content: any;
};
export type Rect = {
  x: number;
  y: number;
  w: number;
  h: number;
  scale: number;
};
export const newRect = () => ({
  x: 0,
  y: 0,
  w: 800,
  h: 600,
  scale: 1,
});
